package compile_and_download

import (
	"log"
	"os"
	"os/exec"
	"test/unit_function/sshdata"
)

type RK3568Libc struct {
	BuildTarget
}

// Compile 编译任务：上传脚本，执行脚本，下载产物
func (receiver *RK3568Libc) Compile() error {
	client := sshdata.GetConnection()
	defer client.Close()
	result := sshdata.UploadFile("/mnt/data/ywx1293154/script_libctest.sh", "script/script_libctest.sh", client, "0744")
	if !result {
		log.Println("upload file script_libctest failed")
		return os.ErrNotExist
	}
	log.Println("uploadFile succeed")
	session, err := client.NewSession()
	if err != nil {
		log.Println("RK3568Libc newSession failed, error=", err)
		return err
	}
	defer session.Close()
	log.Println("executing script_libctest.sh")
	err = session.Run("~/script_libctest.sh > ~/script_libctest.log")
	if err != nil {
		log.Println("RK3568Libc run script failed, error=", err)
		return err
	}
	log.Println("executing script_libctest.sh succeed!")
	return nil
}
func (receiver *RK3568Libc) Download() error {
	// 下载文件并解压
	client := sshdata.GetConnection()
	defer client.Close()
	sshdata.DownloadFile("/mnt/data/ywx1293154/openharmony/out/rk3568/musl.tar.gz", "D:/wsl/ubuntu20/openharmony/out/rk3568/musl.tar.gz", client)
	cmd := exec.Command("tar", "-zxvf", "musl.tar.gz")
	cmd.Dir = "D:/wsl/ubuntu20/openharmony/out/rk3568"
	err := cmd.Run()
	if err != nil {
		log.Println("error occurred when executing tar -zxvf out.tar.gz ", err)
		return err
	}
	log.Println("executing tar -zxvf out.tar.gz succeed")
	log.Println("download out.tar.gz succeed!")
	return nil
}

func (receiver *RK3568Libc) Execute() error {
	// 1.执行测试用例
	log.Println("executing", "libctest")
	cmd := exec.Command(".\\runtest_Windows")
	cmd.Dir = "D:/wsl/ubuntu20/openharmony/third_party/musl/scripts"
	err := cmd.Run()
	if err != nil {
		log.Println("error occurred when executing libctest, error msg = ", err)
		return err
	}
	log.Println("[libctest] libctest execute finished")

	err = cmd.Run()
	// 3.移动文件
	log.Println("[libctest] moving result...", "D:/wsl/ubuntu20/openharmony/third_party/musl/scripts/Summary.txt", "-->", "D:/auto_test/result/libctest/Summary.txt")
	err = os.Rename("D:/wsl/ubuntu20/openharmony/third_party/musl/scripts/Summary.txt", "D:/auto_test/result/libctest/Summary.txt")
	if err != nil {
		log.Println("[libctest]error occurred when moving Summary.txt, error msg = ", err)
		return err
	}
	log.Println("[libctest] moving result...", "D:/wsl/ubuntu20/openharmony/third_party/musl/scripts/REPORT", "-->", "D:/auto_test/result/libctest/REPORT")
	err = os.Rename("D:/wsl/ubuntu20/openharmony/third_party/musl/scripts/REPORT", "D:/auto_test/result/libctest/REPORT")
	if err != nil {
		log.Println("[libctest] error occurred when moving xts_latest.tar.gz, error msg = ", err)
		return err
	}
	log.Println("[libctest] moving result...", "D:/wsl/ubuntu20/openharmony/third_party/musl/scripts/FileList.txt", "-->", "D:/auto_test/result/libctest/FileList.txt")
	err = os.Rename("D:/wsl/ubuntu20/openharmony/third_party/musl/scripts/FileList.txt", "D:/auto_test/result/libctest/FileList.txt")
	if err != nil {
		log.Println("[libctest] error occurred when moving xts_latest.tar.gz, error msg = ", err)
		return err
	}
	log.Println("[libctest] rk3568_xts execute succeed!")
	return nil
}

func (receiver *RK3568Libc) GetDownloadLink() string {
	return "Build Img"
}
